home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / Development / libogg / libvorbis-1.0rc3 / examples / vorbisfile_example.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-27  |  2.9 KB  |  88 lines

  1. /********************************************************************
  2.  *                                                                  *
  3.  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
  4.  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
  5.  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  6.  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
  7.  *                                                                  *
  8.  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001             *
  9.  * by the XIPHOPHORUS Company http://www.xiph.org/                  *
  10.  *                                                                  *
  11.  ********************************************************************
  12.  
  13.  function: simple example decoder using vorbisfile
  14.  last mod: $Id: vorbisfile_example.c,v 1.9 2001/12/20 01:00:24 segher Exp $
  15.  
  16.  ********************************************************************/
  17.  
  18. /* Takes a vorbis bitstream from stdin and writes raw stereo PCM to
  19.    stdout using vorbisfile. Using vorbisfile is much simpler than
  20.    dealing with libvorbis. */
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <math.h>
  25. #include <vorbis/codec.h>
  26. #include <vorbis/vorbisfile.h>
  27.  
  28. #ifdef _WIN32 /* We need the following two to set stdin/stdout to binary */
  29. #include <io.h>
  30. #include <fcntl.h>
  31. #endif
  32.  
  33. char pcmout[4096]; /* take 4k out of the data segment, not the stack */
  34.  
  35. int main(){
  36.   OggVorbis_File vf;
  37.   int eof=0;
  38.   int current_section;
  39.  
  40. #ifdef _WIN32 /* We need to set stdin/stdout to binary mode. Damn windows. */
  41.   /* Beware the evil ifdef. We avoid these where we can, but this one we 
  42.      cannot. Don't add any more, you'll probably go to hell if you do. */
  43.   _setmode( _fileno( stdin ), _O_BINARY );
  44.   _setmode( _fileno( stdout ), _O_BINARY );
  45. #endif
  46.  
  47.   if(ov_open(stdin, &vf, NULL, 0) < 0) {
  48.       fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
  49.       exit(1);
  50.   }
  51.  
  52.   /* Throw the comments plus a few lines about the bitstream we're
  53.      decoding */
  54.   {
  55.     char **ptr=ov_comment(&vf,-1)->user_comments;
  56.     vorbis_info *vi=ov_info(&vf,-1);
  57.     while(*ptr){
  58.       fprintf(stderr,"%s\n",*ptr);
  59.       ++ptr;
  60.     }
  61.     fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi->channels,vi->rate);
  62.     fprintf(stderr,"\nDecoded length: %ld samples\n",
  63.         (long)ov_pcm_total(&vf,-1));
  64.     fprintf(stderr,"Encoded by: %s\n\n",ov_comment(&vf,-1)->vendor);
  65.   }
  66.   
  67.   while(!eof){
  68.     long ret=ov_read(&vf,pcmout,sizeof(pcmout),0,2,1,¤t_section);
  69.     if (ret == 0) {
  70.       /* EOF */
  71.       eof=1;
  72.     } else if (ret < 0) {
  73.       /* error in the stream.  Not a problem, just reporting it in
  74.      case we (the app) cares.  In this case, we don't. */
  75.     } else {
  76.       /* we don't bother dealing with sample rate changes, etc, but
  77.      you'll have to*/
  78.       fwrite(pcmout,1,ret,stdout);
  79.     }
  80.   }
  81.  
  82.   /* cleanup */
  83.   ov_clear(&vf);
  84.     
  85.   fprintf(stderr,"Done.\n");
  86.   return(0);
  87. }
  88.